var baseConfig = require("../../baseConfig");
Page({
  data: {
    limit: 10,
    page: 1,
    value: '',
    isNull: false,
    productList: [],
    layer1Popup: false,
  },
  onLoad: function (options) {
    this.getData()
    this.getEcpm()
  },
  testImg(e) {
    console.log(e.currentTarget.dataset);
    let {
      appid,
      goodsid,
      name,
      test_icon,
      test_name,
    } = e.currentTarget.dataset;
    if (appid == 28) {
      wx.setStorageSync('app_icon', "http://fcdn.qiushilaile.com/tuiwa/tuiwa_app-51051627368789500.png")
    };
    let logo = wx.getStorageSync('app_icon');
    wx.navigateTo({
      url: '/pages/testImg/testImg?appid=' + appid +
        '&logo=' + logo +
        '&goodsId=' + goodsid +
        '&name=' + name +
        '&test_icon=' + test_icon +
        '&test_name=' + test_name,
    })
  },
  bindinput(e) {
    // console.log(e.detail.value);
    let value = e.detail.value.replace(/\s/g, "");
    // console.log(value);
    this.setData({
      value
    })
  },
  search() {
    console.log('输入的搜索值：', this.data.value);
    if (this.data.value == '') {
      wx.showToast({
        title: '请输入关键字',
      })
    } else {
      this.setData({
        productList: [],
        isNull: false,
      });
      wx.request({
        url: baseConfig.baseUrl + 'get_goods_list?name=' + this.data.value + '&limit=' + this.data.limit + '&page=' + 1,
        method: 'POST',
        success: res => {
          console.log(res.data.data.rows);
          if (res.data.data.rows == '') {
            console.log('111');
            this.setData({
              isNull: true,
            })
          }
          this.setData({
            productList: this.data.productList.concat(res.data.data.rows)
          });
        }
      })
    }

  },
  getData() {
    wx.request({
      url: baseConfig.baseUrl + 'get_goods_list?limit=' + this.data.limit + '&page=' + this.data.page + '&name=' + this.data.value,
      method: 'POST',
      success: res => {
        console.log(res.data.data.rows);
        this.setData({
          productList: this.data.productList.concat(res.data.data.rows)
        });
      }
    })
  },
  getEcpm() {
    wx.request({
      url: baseConfig.baseUrl + 'get_yestoday_ecpm',
      method: 'GET',
      success: res => {
        console.log(res.data.data);
        this.setData({
          expectEcpm: res.data.data.expectEcpm.toFixed(2)
        });
      }
    })
  },
  tuiGuang(e) {
    console.log('111', e.currentTarget.dataset);
    let goodsId = e.currentTarget.dataset.goodsid;
    // console.log(goodsId);
    let appid = e.currentTarget.dataset.appid;
    wx.request({
      url: baseConfig.baseUrl + `qrcode/tt/${appid}`,
      method: "GET",
      data: {
        is_weixin: 0,
        goodsId: goodsId
      },
      header: {
        "Authorization": wx.getStorageSync('token'),
        "UID": wx.getStorageSync('userid'),
        'Content-Type': 'application/json'
      },
      success: res => {
        // console.log(res.data.data);
        this.setData({
          layer1Popup: true,
          imgUrl: res.data.data
        })
      }
    })
  },
  douYinCloseClick() {
    this.setData({
      layer1Popup: false
    })
  },
  jiaochengClick() {
    wx.navigateTo({
      url: "../problem/problem"
    })
  },
  saveImage(filePath) {
    var aa = wx.getFileSystemManager();
    aa.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/test.png',
      data: this.data.imgUrl,
      encoding: 'base64',
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/test.png',
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: function (err) {
            console.log(err)
          }
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  saveImageToPhotos(filePath) {
    let self = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              self.saveImage(filePath)
              self.isGetSet = true
            },
            fail() {
              self.isGetSet = false
            }
          })
        } else {
          self.isGetSet = true
          self.saveImage(filePath)
        }
      },
      fail(res) {
        console.log(res);
      }
    })
  },
  onReachBottom: function () { // 页面上拉触底事件的处理函数
    let page = this.data.page + 1
    console.log(page);
    this.setData({
      page: page
    })
    this.getData();
  },
})